package com.chenjiahao.kaiyanwan

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.fragment.app.Fragment
import com.chenjiahao.kaiyanwan.ui.videodetail.VideoDetailActivity

val BUNDLE_VIDEO_DATA = "video_data"
val BUNDLE_CATEGORY_DATA = "category_data"

//腾讯 Bugly APP id
val BUGLY_APPID = "176aad0d9e"

//sp 存储的文件名
val FILE_WATCH_HISTORY_NAME = "watch_history_file"   //观看记录
val FILE_COLLECTION_NAME = "collection_file"    //收藏视屏缓存的文件名


fun Fragment.showToast(content: String): Toast {
    val toast = Toast.makeText(this.activity?.applicationContext, content, Toast.LENGTH_SHORT)
    toast.show()
    return toast
}

fun Activity.showToast(content: String): Toast {
    val toast = Toast.makeText(this?.applicationContext, content, Toast.LENGTH_SHORT)
    toast.show()
    return toast
}

fun Context.showToast(content: String): Toast {
    val toast = Toast.makeText(this?.applicationContext, content, Toast.LENGTH_SHORT)
    toast.show()
    return toast
}

inline  fun <reified T> startActivity(context: Activity, view: View, block: Intent.()->Unit){
    val intent= Intent(context,T::class.java)
    intent.block()
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        val pair = Pair(view, VideoDetailActivity.IMG_TRANSITION)
        val activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
            context, pair)
        ActivityCompat.startActivity(context, intent, activityOptions.toBundle())
    } else {
        context.startActivity(intent)
        context.overridePendingTransition(R.anim.anim_in, R.anim.anim_out)
    }
}

fun durationFormat(duration: Long?): String {
    val minute = duration!! / 60
    val second = duration % 60
    return if (minute <= 9) {
        if (second <= 9) {
            "0$minute' 0$second''"
        } else {
            "0$minute' $second''"
        }
    } else {
        if (second <= 9) {
            "$minute' 0$second''"
        } else {
            "$minute' $second''"
        }
    }
}

/**
 * 数据流量格式化
 */
fun Context.dataFormat(total: Long): String {
    var result: String
    var speedReal: Int = (total / (1024)).toInt()
    result = if (speedReal < 512) {
        speedReal.toString() + " KB"
    } else {
        val mSpeed = speedReal / 1024.0
        (Math.round(mSpeed * 100) / 100.0).toString() + " MB"
    }
    return result
}